package com.cykj.shop.service.impl;

import com.cykj.common.core.web.domain.AjaxResult;
import com.cykj.common.security.utils.SecurityUtils;
import com.cykj.shop.domain.vo.BusinessVo;
import com.cykj.shop.mapper.BusinessMapper;
import com.cykj.shop.service.BusinessService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 叶佳伟
 * @date 2025/8/18
 * @description 功能描述: 商家实现类
 */

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Override
    public AjaxResult updatePassword(BusinessVo businessVo) {
        Integer res1 = businessMapper.isTruePassword(businessVo);
        if(res1 > 0){
            Integer res2 = businessMapper.updatePassword(businessVo);
            if(res2 > 0){
                return AjaxResult.success("密码修改成功");
            }
        }
        return AjaxResult.error("密码验证错误");
    }

    @Override
    public AjaxResult register(BusinessVo businessVo) {
        //判断手机号是否被注册
        Integer res1 = businessMapper.isRegister(businessVo);
        if(res1 > 0){
            return AjaxResult.error("该手机号已被注册");
        }

        //要先判断验证码是否正确
        //后续ocr2要素完成时，注册补一个（姓名+身份号（通过图片方式））

        //密码加密
        businessVo.setPassword(SecurityUtils.encryptPassword(businessVo.getPassword()));

        //手机号未重复，则进行注册
        Integer res2 = businessMapper.register(businessVo);
        if(res2 > 0){
            return AjaxResult.success("商家注册成功");
        }
        return AjaxResult.error("商家注册失败");
    }

    @Override
    public AjaxResult updateAvatar(Integer id, String avatarUrl) {
        Integer res = businessMapper.updateAvatar(id,avatarUrl);
        if(res > 0){
            return AjaxResult.success("店铺头像修改成功");
        }
        return AjaxResult.error("店铺头像修改失败");
    }

}
